package com.zznj.xzjc.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.zznj.xzjc.R;
import com.zznj.xzjc.adapter.index.DictionaryAdapter;
import com.zznj.xzjc.adapter.index.SadsrAdapter;
import com.zznj.xzjc.adapter.index.SadwAdapter;
import com.zznj.xzjc.adapter.index.ZzjgAdapter;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.bean.index.DictionaryData;
import com.zznj.xzjc.bean.index.SadsrData;
import com.zznj.xzjc.bean.index.SadwData;
import com.zznj.xzjc.event.EventTag;
import com.zznj.xzjc.event.MessageEvent;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.AreaBean;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.DiaoChaQuZhengBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.net.entity.ZzjgBean;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.utils.StringUtils;
import com.zznj.xzjc.view.MaxHeightRecyclerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.FormBody;

/**
 * 描述：线索发现
 */
public class ActivityAjcxSadsr extends BaseActivity {


    Unbinder unbinder;


    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.searchBtn)
    ImageView searchBtn;

    @BindView(R.id.xzhj_linear)
    View xzhj_linear;
    @BindView(R.id.xzhj_hj_tv)
    TextView xzhj_hj_tv;
    @BindView(R.id.xzhj_hj_tv_xx)
    ImageView xzhj_hj_tv_xx;


    @BindView(R.id.xzqh_linear)
    View xzqh_linear;
    @BindView(R.id.xzqh_tv)
    TextView xzqh_tv;
    @BindView(R.id.xzqh_tv_xx)
    ImageView xzqh_tv_xx;


    @BindView(R.id.lrdw_linear)
    View lrdw_linear;
    @BindView(R.id.lrdw_tv)
    TextView lrdw_tv;
    @BindView(R.id.lrdw_tv_xx)
    ImageView lrdw_tv_xx;


    @BindView(R.id.dicList)
    MaxHeightRecyclerView dicList;


    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    @BindView(R.id.rec_main)
    RecyclerView rec_main;

    @BindView(R.id.bottomLinear)
    View bottomLinear;
    @BindView(R.id.tishiImg)
    ImageView tishiImg;


    @BindView(R.id.zheCeng)
    View zheCeng;

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_ajcx_sadsr_layout;
    }

    private LoginBean user;

    @Override
    public void initView() {
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);
        setTitle("涉案当事人");
        initDictionary();
        initSrl();
        init();
    }

    private List<SadsrData> list;
    private SadsrAdapter mAdapter;

    private void init() {
        list = new ArrayList<>();
        ((SimpleItemAnimator) rec_main.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        rec_main.setLayoutManager(linearLayoutManager);
        mAdapter = new SadsrAdapter(R.layout.gyss_item_sadsr, list);
        rec_main.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 0);
                bundle.putString("searchNo", list.get(position).getIdsNo());
                RxActivityTool.skipActivity(mContext, ActivityAjcxRelationAj.class, bundle);
            }
        });
    }


    private String sex = "";
    private List<DictionaryData> mzList;
    private int selectMzPosition = 0;

    private String nation = "";
    private List<DictionaryData> whcdList;
    private int selectWhcdPosition = 0;

    private String education = "";
    private List<DictionaryData> xbList;
    private int selectXbPosition = 0;

    private DictionaryAdapter dicAdapter;

    private int type = 0; // 0--性别；1--名族；2--文化程度


    private void initDictionary() {
        xbList = new ArrayList<>();
        DictionaryData qb = new DictionaryData();
        qb.setDtCode("");
        qb.setDtName("性别");

        DictionaryData nan = new DictionaryData();
        nan.setDtCode("1");
        nan.setDtName("男");

        DictionaryData nv = new DictionaryData();
        nv.setDtCode("2");
        nv.setDtName("女");

        xbList.add(qb);
        xbList.add(nan);
        xbList.add(nv);

        mzList = new ArrayList<>();
        whcdList = new ArrayList<>();

        ((SimpleItemAnimator) dicList.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        dicList.setLayoutManager(linearLayoutManager);
        dicAdapter = new DictionaryAdapter(R.layout.gyss_item_select_dictionary, xbList);
        dicList.setAdapter(dicAdapter);
        dicAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                zheCeng.setVisibility(View.GONE);
                dicList.setVisibility(View.GONE);
                if (type == 0) {
                    sex = xbList.get(position).getDtCode();
                    selectXbPosition = position;

                    xzhj_hj_tv.setText(dicAdapter.getData().get(position).getDtName());
                    xzhj_hj_tv.setTextColor(getResources().getColor(R.color.color_333333));
                    xzhj_hj_tv_xx.setImageResource(R.mipmap.gyss_xiala_img);

                    sex = dicAdapter.getData().get(position).getDtCode();
                } else if (type == 1) {
                    nation = mzList.get(position).getDtCode();
                    selectMzPosition = position;
                    xzqh_tv.setTextColor(getResources().getColor(R.color.color_333333));
                    xzqh_tv_xx.setImageResource(R.mipmap.gyss_xiala_img);
                    nation = dicAdapter.getData().get(position).getDtCode();
                    if(StringUtils.isEmpty(nation)){
                        xzqh_tv.setText("民族");
                    }else {
                        xzqh_tv.setText(dicAdapter.getData().get(position).getDtName());
                    }
                } else if (type == 2) {
                    education = whcdList.get(position).getDtCode();
                    selectWhcdPosition = position;
                    lrdw_tv.setTextColor(getResources().getColor(R.color.color_333333));
                    lrdw_tv_xx.setImageResource(R.mipmap.gyss_xiala_img);

                    education = dicAdapter.getData().get(position).getDtCode();
                    if(StringUtils.isEmpty(education)){
                        lrdw_tv.setText("文化程度");
                    }else {
                        lrdw_tv.setText(dicAdapter.getData().get(position).getDtName());
                    }
                }
                selectImg = null;
                selectLevel = null;
                refresh();
            }
        });
    }

    @Override
    protected void initData() {
        getDictionary("nation");
        getDictionary("educationLevel");
        refresh();
    }

    private void getDictionary(String groupCode) {
        SubscriberOnNextListener<BaseBean<List<DictionaryData>>> onNextListener = baseBean -> {
            if ("nation".equals(groupCode)) {
                mzList = baseBean.getResult();
                DictionaryData dic = new DictionaryData();
                dic.setDtCode("");
                dic.setDtName("民族(全部)");
                mzList.add(0, dic);
            } else {
                whcdList = baseBean.getResult();
                DictionaryData dic = new DictionaryData();
                dic.setDtCode("");
                dic.setDtName("文化程度(全部)");
                whcdList.add(0, dic);
            }
        };
        FormBody requestBody = new FormBody.Builder()
                .add("groupCode", groupCode)
                .build();
        HttpMethods.getInstance().getDicByKey(new ProgressSubscriber<>(onNextListener, mContext, false), requestBody);
    }

    private void initSrl() {
        srlAll.setEnableRefresh(true);
        srlAll.setEnableLoadMore(true);
        srlAll.setEnableAutoLoadMore(true);
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh();
        });
        srlAll.setOnLoadMoreListener(refresh_layout -> {
            isShowDialog = false;
            page++;
            getData();
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshData(MessageEvent messageEvent) {
        if (messageEvent.getTag().equals(EventTag.DT)) {
            refresh();
        }
    }

    private boolean isShowDialog = true;
    private int page = 0;
    private List<Object> lcjdListData = new ArrayList<>();

    public void refresh() {
        srlAll.setEnableLoadMore(true);
        page = 1;
        srlAll.setEnableLoadMore(true);
        list.clear();
        bottomLinear.setVisibility(View.GONE);
        mAdapter.notifyDataSetChanged();
        getData();
    }

    private void getData() {
        SubscriberOnNextListener<BaseBean<List<SadsrData>>> onNextListener = baseBean -> {
            if (srlAll != null) {
                srlAll.finishLoadMore(true);
                srlAll.finishRefresh(true);
            }
            if (list.size() + baseBean.getResult().size() > 0 && baseBean.getResult().size() < 10) {
                bottomLinear.setVisibility(View.VISIBLE);
                if (srlAll != null) {
                    srlAll.setEnableLoadMore(false);
                }
            }
            list.addAll(baseBean.getResult());
            mAdapter.notifyDataSetChanged();


            if (mAdapter.getItemCount() == 0) {
                tishiImg.setVisibility(View.VISIBLE);
                srlAll.setVisibility(View.GONE);
                tishiImg.setImageResource(R.mipmap.gyss_zwnr_img);
            } else {
                tishiImg.setVisibility(View.GONE);
                srlAll.setVisibility(View.VISIBLE);
            }
        };


        FormBody requestBody = new FormBody.Builder()
                .add("condition", searchEdit.getText().toString())
                .add("sex", sex)
                .add("nation", nation)
                .add("education", education)
                .add("page", page+"")
                .build();
        HttpMethods.getInstance().suspectSearch(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    private TextView selectLevel;
    private ImageView selectImg;

    @OnClick({R.id.xzhj_linear, R.id.xzqh_linear, R.id.lrdw_linear,R.id.searchBtn})
    public void onClick(View v) {
        if (v.getId() == R.id.xzhj_linear) {
            if (selectLevel == xzhj_hj_tv) {
                selectLevel.setTextColor(getResources().getColor(R.color.color_333333));
                selectImg.setImageResource(R.mipmap.gyss_xiala_img);
                zheCeng.setVisibility(View.GONE);
                dicList.setVisibility(View.GONE);
                selectLevel = null;
                selectImg = null;
            } else {
                type = 0;
                dicAdapter.setNewData(xbList);
                dicAdapter.setSelectPosition(selectXbPosition);

                zheCeng.setVisibility(View.VISIBLE);
                dicList.setVisibility(View.VISIBLE);

                xzhj_hj_tv.setTextColor(getResources().getColor(R.color.title_bg));
                xzhj_hj_tv_xx.setImageResource(R.mipmap.gyss_xiangxia_select);

                selectLevel = xzhj_hj_tv;
                selectImg = xzhj_hj_tv_xx;
            }
        } else if (v.getId() == R.id.xzqh_linear) {
            if (selectLevel == xzqh_tv) {
                selectLevel.setTextColor(getResources().getColor(R.color.color_333333));
                selectImg.setImageResource(R.mipmap.gyss_xiala_img);
                zheCeng.setVisibility(View.GONE);
                dicList.setVisibility(View.GONE);
                selectLevel = null;
                selectImg = null;
            } else {
                type = 1;
                dicAdapter.setNewData(mzList);
                dicAdapter.setSelectPosition(selectMzPosition);

                zheCeng.setVisibility(View.VISIBLE);
                dicList.setVisibility(View.VISIBLE);

                xzqh_tv.setTextColor(getResources().getColor(R.color.title_bg));
                xzqh_tv_xx.setImageResource(R.mipmap.gyss_xiangxia_select);

                selectLevel = xzqh_tv;
                selectImg = xzqh_tv_xx;
            }
        } else if (v.getId() == R.id.lrdw_linear) {
            if (selectLevel == lrdw_tv) {
                selectLevel.setTextColor(getResources().getColor(R.color.color_333333));
                selectImg.setImageResource(R.mipmap.gyss_xiala_img);
                zheCeng.setVisibility(View.GONE);
                dicList.setVisibility(View.GONE);
                selectLevel = null;
                selectImg = null;
            } else {
                type = 2;
                dicAdapter.setNewData(whcdList);
                dicAdapter.setSelectPosition(selectWhcdPosition);

                zheCeng.setVisibility(View.VISIBLE);
                dicList.setVisibility(View.VISIBLE);

                lrdw_tv.setTextColor(getResources().getColor(R.color.title_bg));
                lrdw_tv_xx.setImageResource(R.mipmap.gyss_xiangxia_select);

                selectLevel = lrdw_tv;
                selectImg = lrdw_tv_xx;
            }
        }else if(v.getId() == R.id.searchBtn){
            refresh();
        }
    }
}
